/**
 * @file models/schedulingDetails.js
 * @author
 */
import { routerRedux } from 'dva/router';
import api from '../api';
import moment from 'moment'


export default {
	namespace: 'schedulingDetails',
	state: {
		filter:'all',
		showModal:false,
		jobInfo:null,
		errorDistribute:null,
		jobHistoryAll:[],
		jobHistory:[],
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({ pathname, query }) => {
				// 没有参数时，向后端请求数据，并且改变query
				if( pathname==='/systemMgt/operationMgt/taskMgtDetails' ){
					dispatch({type: 'whetherRequest',payload:query,pathname})
				}
			})
		}
	},
	effects : {

		* whetherRequest({
				payload,pathname
			}, { put,select}) {
				const state=yield select(state=>state.schedulingDetails)
				if(!state.jobList){
					yield put({ type:'getJobList'})
				}
				yield put({
					type:'setState',
					payload:{
						jobName:payload.jobName
					}
				})
				if(payload.jobName){
					if(state.jobName!=payload.jobName){
						yield [
							put({type:'clearState'}),
							put({
								type:'query',
								payload,
							}),

						]
					}
				}


		},
		* getJobList({payload}, {call, put,select}){
			const jobList=yield call(api.scheduling.getJobList, payload)
			yield put({
				type:'setState',
				payload:{
					jobList:jobList.data
				}
			})
		},
		* query({payload}, {call, put,select}){
			const [
				jobInfo,
				errorDistribute,
				jobHistoryAll,
			] = yield [
				call(api.scheduling.getJobInfo, payload),
				call(api.scheduling.getJobErrorDistribute, payload),
				call(api.scheduling.getJobHistory, payload),
			];

			yield put({
				type: 'querySuccess',
				payload:{
					jobID:jobHistoryAll.data[0].id,
					jobInfo:jobInfo.data,
					errorDistribute:errorDistribute.data,
					jobHistoryAll:jobHistoryAll.data,
					jobHistory:jobHistoryAll.data,
				},
			});

			yield put({
				type:'getDetails',
				payload:{
					jobID:jobHistoryAll.data[0].id,
				},
			});

		},
		* filterData({payload}, {put, select}){
			const state=yield select(state=>state.schedulingDetails)
			let jobHistory
			if(payload.filter==='all'){
				jobHistory=state.jobHistoryAll
			}else{
				jobHistory=state.jobHistoryAll.filter(item=>item.jobResult===payload.filter)
			}
			yield put({
				type: 'querySuccess',
				payload:{
					jobHistory:jobHistory,
					filter:payload.filter,
				},
			});
		},
		* getDetails({payload}, {put,call}){
			const record=yield call(api.scheduling.getJobReport, payload);
			yield put({
				type: 'querySuccess',
				payload:{
					jobID:payload.jobID,
					records:record.data,
				},
			});
		},
		* addJobHandling({payload}, {put,call,select}){
			const state=yield select(state=>state.schedulingDetails)
			const addJobResult=yield call(api.scheduling.addJobHandling, payload);
			state.records.handleRecord=addJobResult.data
			yield put({
				type: 'querySuccess',
				payload:{
					records:state.records,
				},
			});
		},
		* changeRouter({payload}, {put}){
			yield put(routerRedux.push(payload));
		}

	},
	reducers : {
		setState(state, {payload}) {
			return {
				...state,
				...payload
			}
		},

		clearState(state) {
			const {
				errorDistribute,
				jobHistory,
				jobHistoryAll,
				jobID,
				jobInfo,
				records,
				showModal,
				filter,
				...other
			}=state
      return{
				...other
			}
    },
		querySuccess(state, {payload}) {
			return {
				...state,
				...payload
			}
		}

	}
};
